mui.init();

var old_back = mui.back;
mui.back = function(){
	var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);	
	if(!targetPageId){
		mui.alert("参数：targetPageId 为 null ,无法跳转到下一步");
		return;
	}
	var targetPage = plus.webview.getWebviewById(targetPageId);
    mui.fire(targetPage,'reload',{});
    targetPage.show();
}

mui.plusReady(function(){

	loadDatas();	
	var btn_submit = CmwMcore.$("btn_submit");
	MeventMgr.onTouchstart(btn_submit,function(event,self){
		if(!valid()) return;
		submitForm();
	});
		
	function valid(){
		var newNickName = $("#newNickName").val().trim();
		var nickName =  $("#nickName").val().trim();
		if(!newNickName){
			mui.alert("请输入新昵称!");
			return false;
		}
		if(newNickName==nickName){
			mui.alert("新昵称和原昵称相同啦!");
			return false;
		}
		if(newNickName.length >= 20){
			mui.alert("昵称长度不能超过20个字符!");
			return false;
		}
		return true;
	}
	
	function submitForm(){
		var newNickName = $("#newNickName").val();
		var pars={nickName:newNickName};
		var url=UrlConfig.updateNickName;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if(status == "true"){
		  			mui.alert("修改成功!");
		  			mui.back();
		  		}else{
		  			mui.alert("修改失败!");
		  		}
		  	},ffn : function(err){
		  		mui.alert("获取数据失败");
		  	}
		});
	}
 
});

function loadDatas(){
	var url = UrlConfig.getUserInfo;
	var guid = plus.storage.getItem(KeyCfg.userId);
	var pars={guid:guid};
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
	  		var user =data;
	  		//var userName = user.userName;
	  		var nickName = user.nickName;
	  		//$("#name").val(userName);
	  		$("#nickName").val(nickName);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  		//mui.plus.nativeUI.closeWaiting();
	  	}
	 });
}
